var phonetestcode = 2;
var passwordtestcode = 2;
$(document).ready(function(){
	login();
	KeyBoardClick();
	
});
//捕捉键盘点击事件
var KeyBoardClick = function(){
	/*获得键盘上对应的 ascII 码*/
	/*$(document).keydown(function (event) {
	    alert(event.keyCode);
	});*/
	$(document).keydown(function (event) {
	    
	    switch (event.keyCode) {
	    case 13:
	    	$("div.submit").click();//键盘点击Enter键提交登录
	        break;
	    
	    };
//	    return false;//阻止键盘上任何按键操作
	});
	layer.close(LoadingIcon);
}
//点击登录
function login(){
	$("div.submit").click(function(){
		 if(logintype=='plogin'){
			 PhoneLogin();
         }
         if(logintype=='uplogin'){
             var upphone=$('#upphone').val();
             var pswd=$('#pswd').val();
             var xcode = $("input#upvcode").val();
             $.ajax({
                 url:basePath+'/meb/login',
                 data:{
                     phone:upphone,
                     password:pswd,
                     scode:xcode
                 },
                 type:'post',
                 dataType:'json',
                 success:function(data){
                	 console.log(data);
                     var code = data.code;
                     if(data.code == "1"){
                    	var url = basePath + '/loginsteptwo';
                     	url = encodeURI(url);
                     	url = encodeURI(url);
                     	window.location.href = (url);
                     }else if(data.code == "2"){
                    	 layer.alert("请输入正确的验证码或密码",{icon: 2,title:"温馨提示"});
                     }else{
                    	 window.location.href = (basePath+"/index");
                     }
                 },
                 error:function(data){

                 },
                 complete:function (data) {

                 }
             })
         }
         //登录成功之后如有跳转，比如登录成功就跳转首页，在后台接口完成此逻辑
	})          
}

//手机号码登录
var PhoneLogin = function(){
	var phone=$('#phone').val();
    var code=$('#pcode').val();
    var scode = $("input#vcode").val();
    console.log("开始验证手机登录");
    $.ajax({
        url:basePath+'/meb/login',
        data:{
            phone:phone,
            code:code,
            scode:scode
        },
        type:'post',
        dataType:'json',
        success:function(data){
            console.log(data);
            var code = data.code;
            if(code == "1" && phonetestcode == 1){
           	var url = basePath + '/loginsteptwo';
           	url = encodeURI(url);
           	url = encodeURI(url);
           	window.location.href = (url);
            }else if(code == "2" || phonetestcode == 2){
//           	 alert("请输入正确的验证码或密码");
           	 layer.alert("请输入正确的验证码或密码",{icon: 2,title:"温馨提示"});
            }
        },
        error:function(data){
				console.log("验证失败");
        },
        complete:function (data) {

        }
    })
}
//发送手机验证码
var CheckPhoneCode = function(){
	var phone=$("#phone").val();
    $.ajax({
        url:basePath+'/meb/phonecode',
        data:{
            phone:phone
        },
        type:'post',
        dataType:'json',
        success:function(data){
        	if(data.code == "1"){
        		layer.msg("手机验证码发送成功，请注意查收");
                console.log(data);
                var count = 60;
                var interval = setInterval(function(){
                	count -= 1;
                	var detail = count + "s后可重发";
                	$("input.sendpcode").val(detail);
                	$("input.sendpcode").attr("disabled",true);
                	if(count === 0){
                		$("input.sendpcode").val("发送验证码");
                		$("input.sendpcode").attr("disabled",false);
                		clearInterval(interval);
                	};  	
                },1000);
        	}else{
        		layer.msg("手机验证码发送失败，请检查手机号码是否有误");
        	}
        	
        },
        error:function(data){
        	layer.msg("验证码发送失败，请检查网络连接");
        },
        complete:function (data) {

        }
    })
}